package com.party.mobile.web.controller.analyze;

import com.google.common.collect.Maps;
import com.party.authorization.annotation.Authorization;
import com.party.common.paging.Page;
import com.party.core.model.main.MainViewAnalyze;
import com.party.core.model.member.MemberMerchantDetail;
import com.party.core.model.member.MemberMerchantDetailQuery;
import com.party.core.service.main.IMainViewAnalyzeService;
import com.party.mobile.biz.analyze.AnalyzeBizService;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.analyze.output.AllDataOutput;
import com.party.mobile.web.dto.analyze.output.AnalyzeListOutput;
import com.party.mobile.web.dto.analyze.output.CopartnerAnalyzeListOutput;
import com.party.mobile.web.dto.analyze.output.MerchantAnalyzeListOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据分析
 * Created by wei.li
 *
 * @date 2017/9/7 0007
 * @time 10:31
 */

@Controller
@RequestMapping(value = "analyze")
public class AnalyzeController {

    @Autowired
    private CurrentUserBizService currentUserBizService;

    @Autowired
    private AnalyzeBizService analyzeBizService;

    @Autowired
    private IMainViewAnalyzeService mainViewAnalyzeService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 统计列表
     * @param request 请求参数
     * @return 交互数据
     */
    @Authorization
    @ResponseBody
    @RequestMapping(value = "list")
    public AjaxResult list(HttpServletRequest request){
        String memberId = currentUserBizService.getCurrentUser(request).getId();
        Map<String, AnalyzeListOutput> map = analyzeBizService.list(memberId);
        return AjaxResult.success(map);
    }

    /**
     * 统计列表
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "listByMember")
    public AjaxResult listByMember(String id){
        Map<String, AnalyzeListOutput> map = analyzeBizService.list(id);
        return AjaxResult.success(map);
    }

    /**
     * 视图
     * @param request 请求参数
     * @return 交互数据
     */
    @Authorization
    @ResponseBody
    @RequestMapping(value = "chart")
    public AjaxResult chart(HttpServletRequest request){
        HashMap<String, List> map = Maps.newHashMap();
        String memberId = currentUserBizService.getCurrentUser(request).getId();
        List<String> dateList = analyzeBizService.dateStringList();

        List<MemberMerchantDetail> list = analyzeBizService.analyzeList(memberId);
        List<String> applyList = analyzeBizService.applyList(list);
        List<String> orderList = analyzeBizService.orderList(list);
        List<String> paymentList = analyzeBizService.paymentList(list);
        List<String> viewList = analyzeBizService.viewList(list);
        List<String> memberList = analyzeBizService.memberList(list);
        map.put("dateList", dateList);
        map.put("applyList", applyList);
        map.put("orderList", orderList);
        map.put("paymentList", paymentList);
        map.put("viewList", viewList);
        map.put("memberList", memberList);
        return AjaxResult.success(map);
    }

    /**
     * 单位视图
     * @param request 请求参数
     * @param unit 单位（0:7天 1:6周 2:6个月 3:12个月）
     * @return 交互数据
     */
    @Authorization
    @ResponseBody
    @RequestMapping(value = "chartForUnit")
    public AjaxResult chartForUnit(HttpServletRequest request, Integer unit){
        AjaxResult ajaxResult = new AjaxResult(true);
        unit = null == unit ? 0 : unit; //默认是7天
        String memberId = currentUserBizService.getCurrentUser(request).getId();
        List<String> dateList = analyzeBizService.dateStringList(unit);
        List<MemberMerchantDetail> list = analyzeBizService.analyzeList(memberId, unit);
        HashMap<String, List> map = analyzeBizService.getOutPutMap(list, unit);
        map.put("dateList", dateList);
        ajaxResult.setData(map);
        return ajaxResult;
    }

    /**
     * 视图
     * @return 交互数据
     */
    @Authorization
    @ResponseBody
    @RequestMapping(value = "chartByMember")
    public AjaxResult chartByMember(String id){
        HashMap<String, List> map = Maps.newHashMap();
        List<String> dateList = analyzeBizService.dateStringList();

        List<MemberMerchantDetail> list = analyzeBizService.analyzeList(id);
        List<String> applyList = analyzeBizService.applyList(list);
        List<String> orderList = analyzeBizService.orderList(list);
        List<String> paymentList = analyzeBizService.paymentList(list);
        List<String> viewList = analyzeBizService.viewList(list);
        List<String> memberList = analyzeBizService.memberList(list);
        map.put("dateList", dateList);
        map.put("applyList", applyList);
        map.put("orderList", orderList);
        map.put("paymentList", paymentList);
        map.put("viewList", viewList);
        map.put("memberList", memberList);
        return AjaxResult.success(map);
    }

    /**
     * 根据用户查询单位视图
     * @param id 用户编号
     * @param unit 单位（0:7天 1:6周 2:6个月 3:12个月）
     * @return 交互数据
     */
    @Authorization
    @ResponseBody
    @RequestMapping(value = "chartForUnitByMember")
    public AjaxResult chartForUnitByMember(String id, Integer unit){
        AjaxResult ajaxResult = new AjaxResult(true);
        unit = null == unit ? 0 : unit; //默认是7天
        List<String> dateList = analyzeBizService.dateStringList(unit);
        List<MemberMerchantDetail> list = analyzeBizService.analyzeList(id, unit);
        HashMap<String, List> map = analyzeBizService.getOutPutMap(list, unit);
        map.put("dateList", dateList);
        ajaxResult.setData(map);
        return ajaxResult;
    }

    /**
     * 统计列表
     * @param request 请求参数
     * @return 交互数据
     */
    @Authorization
    @ResponseBody
    @RequestMapping(value = "copartner/list")
    public AjaxResult copartnerList(HttpServletRequest request){
        String memberId = currentUserBizService.getCurrentUser(request).getId();
        Map<String, CopartnerAnalyzeListOutput> map = analyzeBizService.copartnerList(memberId);
        return AjaxResult.success(map);
    }


    /**
     * 所有机构统计
     * @param memberMerchantDetailQuery 查询参数
     * @param page 分页参数
     * @return 返回数据
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "all/list")
    public AjaxResult allList(MemberMerchantDetailQuery memberMerchantDetailQuery, Page page, HttpServletRequest request) {
        try {
            String memberId = currentUserBizService.getCurrentUser(request).getId();
            List<MerchantAnalyzeListOutput> list = analyzeBizService.allList(memberMerchantDetailQuery, page, memberId);
            return AjaxResult.success(list, page);
        } catch (Exception e) {
            logger.error("所有机构统计异常", e);
            return AjaxResult.error(100, "所有机构统计异常");
        }
    }


    /**
     * 总计统计数据
     * @return 数据
     */
    @ResponseBody
    @RequestMapping(value = "admin/detail")
    public AjaxResult adminDetail(){
        Map<String, MainViewAnalyze> map = analyzeBizService.adminDetail();
        return AjaxResult.success(map);
    }


    /**
     * 统计数据
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "all/chart")
    public AjaxResult allChart(){
        HashMap<String, List> map = Maps.newHashMap();
        List<String> dateList = analyzeBizService.dateStringList();

        List<MemberMerchantDetail> list = analyzeBizService.analyzeAdminList();
        List<String> applyList = analyzeBizService.applyList(list);
        List<String> orderList = analyzeBizService.orderList(list);
        List<String> paymentList = analyzeBizService.paymentList(list);
        List<String> viewList = analyzeBizService.viewList(list);
        List<String> memberList = analyzeBizService.memberList(list);
        map.put("dateList", dateList);
        map.put("applyList", applyList);
        map.put("orderList", orderList);
        map.put("paymentList", paymentList);
        map.put("viewList", viewList);
        map.put("memberList", memberList);
        return AjaxResult.success(map);
    }

    /**
     * 根据单元统计
     * @param unit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "all/chartForUnit")
    public AjaxResult allChartForUnit(Integer unit){
        unit = null == unit ? 0 : unit; //默认是7天
        List<String> dateList = analyzeBizService.dateStringList(unit);
        List<MemberMerchantDetail> list = analyzeBizService.analyzeAdminList(unit);
        HashMap<String, List> map = analyzeBizService.getOutPutMap(list, unit);
        map.put("dateList", dateList);
        return AjaxResult.success(map);
    }

    /**
     * 所有统计数据
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "all/data")
    public AjaxResult allData() {
        try {
            MainViewAnalyze mainView = mainViewAnalyzeService.getMainView(new MainViewAnalyze(0, "total", "SYSTEM"));

            MainViewAnalyze history = mainViewAnalyzeService.getMainView(new MainViewAnalyze(0, "history", "SYSTEM"));

            AllDataOutput allDataOutput = new AllDataOutput();
            allDataOutput.setApplyNum(mainView.getApplyNum());
            allDataOutput.setOrderNum(mainView.getOrderNum());
            allDataOutput.setViewNum(history.getViewNum());
            allDataOutput.setMemberNum(mainView.getMemberNum());
            allDataOutput.setMoney(mainView.getMoney());
            return AjaxResult.success(allDataOutput);
        } catch (Exception e) {
            logger.error("获取所有统计数据异常", e);
            return AjaxResult.error(100, "获取所有统计数据异常");
        }
    }
}
